package com.aifast.system.sys.controller;

import com.aifast.common.base.Controller;
import com.aifast.common.base.R;
import com.aifast.system.sys.service.SseEmitterService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * @Author fujian
 * @since 2022/10/18 10:24
 */
@RequestMapping("sse")
@RestController
@RequiredArgsConstructor
public class SseEmitterController extends Controller {

    private final SseEmitterService service;

    @GetMapping(path = "sub",produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
    public SseEmitter getConnect(){
        return service.connect(get("clientId"));
    }

    @GetMapping("push")
    public R push(){
        return service.send(get("clientId"),get("msg")) ? OK() : ERROR();
    }

    @GetMapping("closeConnect")
    public R closeConnect(){
        service.close(get("clientId"));
        return OK();
    }

}
